package com.ruoyi.patrol.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.service.WebSocket;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.patrol.constants.ReStatusEnum;
import com.ruoyi.patrol.domain.PtWorkRepair;
import com.ruoyi.patrol.vo.PtWorkPatrolVO;
import com.ruoyi.patrol.vo.PtWorkReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.patrol.mapper.PtWorkReportMapper;
import com.ruoyi.patrol.domain.PtWorkReport;
import com.ruoyi.patrol.service.IPtWorkReportService;

/**
 * 记录工单信息Service业务层处理
 *
 * @author linan
 * @date 2024-02-03
 */
@Service
public class PtWorkReportServiceImpl implements IPtWorkReportService
{
    @Autowired
    private PtWorkReportMapper ptWorkReportMapper;
    @Autowired
    private WebSocket webSocket;


    /**
     * 查询记录工单信息
     *
     * @param id 记录工单信息主键
     * @return 记录工单信息
     */
    @Override
    public PtWorkReportVO selectPtWorkReportById(String id)
    {
        PtWorkReportVO ptWorkReportVO = ptWorkReportMapper.selectPtWorkReportById(id);
        format(ptWorkReportVO);
        return ptWorkReportVO;
    }

    /**
     * 查询记录工单信息列表
     *
     * @param ptWorkReport 记录工单信息
     * @return 记录工单信息
     */
    @Override
    public List<PtWorkReportVO> selectPtWorkReportList(PtWorkReport ptWorkReport)
    {
        List<PtWorkReportVO> ptWorkReportVOS = ptWorkReportMapper.selectPtWorkReportList(ptWorkReport);
        ptWorkReportVOS.forEach(this::format);
        return ptWorkReportVOS;
    }

    private void format(PtWorkReportVO x) {
        String statusText = x.getStatusText();
        if (StringUtils.isNotEmpty(statusText)) {
            JSONArray array = new JSONArray(statusText);
            Map<Integer, JSONObject> map = new HashMap<>(3);
            for (int i = 0; i < array.size(); i++) {
                JSONObject jsonObject = array.getJSONObject(i);
                map.put(jsonObject.getInt("statusType"), jsonObject);
            }
            x.setStatusJson(map.get(0) == null ? null : map.get(0).toString());
            x.setStatus2Json(map.get(2) == null ? null : map.get(2).toString());
            x.setStatus3Json(map.get(3) == null ? null : map.get(3).toString());
            x.setStatus4Json(map.get(4) == null ? null : map.get(4).toString());
        }
    }

    /**
     * 新增记录工单信息
     *
     * @param ptWorkReport 记录工单信息
     * @return 结果
     */
    @Override
    public int insertPtWorkReport(PtWorkReport ptWorkReport)
    {
        ptWorkReport.setId(IdUtil.getSnowflakeNextIdStr());
        ptWorkReport.setStatus(ReStatusEnum.INIT.getCode());
        ptWorkReport.setReStatus(ReStatusEnum.INIT.getCode());
        int report = ptWorkReportMapper.insertPtWorkReport(ptWorkReport);
        webSocket.sendMessage(ptWorkReport.getRepairPerson(), ptWorkReport.getId());
        return report;
    }

    /**
     * 修改记录工单信息
     *
     * @param ptWorkReport 记录工单信息
     * @return 结果
     */
    @Override
    public int updatePtWorkReport(PtWorkReport ptWorkReport)
    {
        return ptWorkReportMapper.updatePtWorkReport(ptWorkReport);
    }

    /**
     * 批量删除记录工单信息
     *
     * @param ids 需要删除的记录工单信息主键
     * @return 结果
     */
    @Override
    public int deletePtWorkReportByIds(Long[] ids)
    {
        return ptWorkReportMapper.deletePtWorkReportByIds(ids);
    }

    /**
     * 删除记录工单信息信息
     *
     * @param id 记录工单信息主键
     * @return 结果
     */
    @Override
    public int deletePtWorkReportById(Long id)
    {
        return ptWorkReportMapper.deletePtWorkReportById(id);
    }

    @Override
    public void updateByEndTime(ReStatusEnum reStatusEnum) {
        ptWorkReportMapper.updateByEndTime(reStatusEnum.getCode());
    }

    @Override
    public AjaxResult updateStatus(Map<String, Object> param) {
        Long id = MapUtil.getLong(param, "id");
        PtWorkReportVO ptWorkReportVO = ptWorkReportMapper.selectPtWorkReportById(String.valueOf(id));
        if (ObjectUtil.equal(ptWorkReportVO.getStatus(), 1)) {
            return AjaxResult.error("当前档案已处理完成，请勿重复操作！");
        }
        Integer status = MapUtil.getInt(param, "status");
        Integer status2 = MapUtil.getInt(param, "status2");
        Integer status3 = MapUtil.getInt(param, "status3");
        Integer status4 = MapUtil.getInt(param, "status4");
        Integer reStatus = MapUtil.getInt(param, "reStatus");
        String user = MapUtil.getStr(param, "user");
        String remark = MapUtil.getStr(param, "remark");
        String video = MapUtil.getStr(param, "video");
        String img = MapUtil.getStr(param, "img");
        String endDate = MapUtil.getStr(param, "endDate");
        String repairPerson = MapUtil.getStr(param, "repairPerson");
        String repairPersonPhone = MapUtil.getStr(param, "repairPersonPhone");
        int statusType;
        if (status == null) {
            if (status2 != null) {
                statusType = 2;
                status = status2;
                if (status2 == 2) {
                    PtWorkReport workReport = new PtWorkReport();
                    workReport.setId(id.toString());
                    workReport.setStatus(1);
                    updatePtWorkReport(workReport);
                }
            } else if (status3 != null) {
                statusType = 3;
                status = status3;
            } else if (status4 != null) {
                statusType = 4;
                status = status4;
            } else {
                return AjaxResult.error("错误传参！");
            }
        } else {
            statusType = 0;
        }
        String str = ptWorkReportVO.getStatusText();
        JSONArray array;
        if (StringUtils.isNotEmpty(str)) {
            array = new JSONArray(str);
        } else {
            array = new JSONArray();
        }
        JSONObject entries = new JSONObject();
        entries.set("user", user)
                .set("statusType", statusType)
                .set("remark", remark)
                .set("status", status)
                .set("reStatus", reStatus)
                .set("video", video)
                .set("img", img)
                .set("endDate", endDate)
                .set("repairPerson", repairPerson)
                .set("repairPersonPhone", repairPersonPhone)
                .set("time", DateUtil.now());
        array.add(entries);
        int i = ptWorkReportMapper.updateStatus(id, statusType, status, array.toString(), endDate, repairPerson, repairPersonPhone);
        return AjaxResult.status(i > 0);
    }

    @Override
    public AjaxResult position(Long id, String lon, String lat, int buffer) {
        return AjaxResult.status(ptWorkReportMapper.position(id, lon, lat, buffer), "打卡成功！", "不在打卡区域内，打卡失败！");
    }
}
